#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   status.sh
# Version:    1.0
# Date:       2013/12/05
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    03系统安全功能-03IPTables-02IPTables基本策略和简单规则配置-01查看防火墙默认规则
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/12/05
#             - iptables status.
# ----------------------------------------------------------------------

##! @TODO:  iptables status
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoTest()
{
    trap DoCleanup INT EXIT
    serv=iptables
    CheckServiceExists  ${serv}
    EchoResult "check service"

    CONFIG=/etc/sysconfig/iptables

    while true; do
        StatusDaemon ${serv}

        # if stop,then start
        if [ $? -ne 0 ]
        then
            EchoInfo "${serv} is stopped"
            if [ ! -e ${CONFIG} ];then
                [ -e "./iptables" ] && cp ./iptables ${CONFIG}
                [ -e ${CONFIG} ]
                EchoResult "${CONFIG} exists"
            fi
                StartDaemon ${serv}
                EchoResult "service ${serv} start"
                break
        else
            EchoInfo "${serv} is running..."
            StopDaemon ${serv}
            EchoResult "stop ${serv}"
            continue
        fi

        # check iptablesd start
        StatusDaemon ${serv} && lsmod | grep ip_tables
        EchoResult "${serv} : start success"
    done


}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function DoCleanup()
{
    StopDaemon ${serv}
    EchoResult  "stop ${serv}"
    rm -f ${CONFIG}
}

# include lib files
if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/NetOps.sh

DoTest
